package com.xmgl.daoImpl;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.stereotype.Repository;

import com.xmgl.dao.TestDao;
import com.xmgl.model.TestModel;

@Repository
public class TestDaoImpl implements TestDao {

	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Override
	public List<TestModel> findUsers() {
		// TODO Auto-generated method stub
		List<TestModel> list = mongoTemplate.find("users", new Query(), TestModel.class);
		
		return list;
	}

	@Override
	public List<TestModel> findUsers(String query) {
		// TODO Auto-generated method stub
		Pattern pattern = Pattern.compile(query,Pattern.CASE_INSENSITIVE);
		Criteria criteria = new Criteria("hobby").regex(pattern.toString());
		Query q = new Query(criteria);		
		List<TestModel> list = mongoTemplate.find("users",q, TestModel.class);
		return list;
	}

}
